package com.zcf.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zcf.common.json.Body;
import com.zcf.mapper.MyCouponMapper;
import com.zcf.pojo.Coupon;
import com.zcf.pojo.Dining;
import com.zcf.pojo.MyCoupon;
import com.zcf.pojo.User;
import com.zcf.utils.DateUtil;
import com.zcf.utils.Hutool;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-11-28
 */
@Controller
@RequestMapping("/myCoupon")
@CrossOrigin
@ResponseBody
public class MyCouponController {

	@Autowired
	MyCouponMapper mycouponMapper;

	/**
	 * 用户自身查询优惠劵
	 */
	@PostMapping("query")
	public Body query(String userId, String storeId) {
		EntityWrapper<MyCoupon> ew = new EntityWrapper<>();
		ew.eq("user_id", userId);
		ew.orderBy("time", false);
		if (storeId != null && storeId != "") {
			ew.eq("din_id", storeId);
		}

		List<MyCoupon> lists = mycouponMapper.selectList(ew);
		List<MyCoupon> list = new ArrayList<>();
		for (MyCoupon myCoupon : lists) {
			Dining dining = new Dining();
			if (myCoupon.getDinId() != null && myCoupon.getDinId().length() > 0) {
				dining = dining.selectById(myCoupon.getDinId());
				myCoupon.setDinImg(dining.getLogo());
				myCoupon.setDinName(dining.getDiningName());
				myCoupon.setDesc(1);
				if(myCoupon.getUse() ==0){
					list.add(myCoupon);
				}

				continue;
			}
			myCoupon.setDesc(0);
			if(myCoupon.getUse() ==0){
				list.add(myCoupon);
			}

			continue;
		}
		if (list.size() > 0) {
			return Body.newInstance(list);
		}
		return Body.newInstance("查无数据");
	}

	/**
	 * 领取优惠劵
	 */
	@PostMapping("insertMyCoupon")
	public Body insertMyCoupon(String couponId, String userId, String sum) {
		Coupon coupon = new Coupon();
		coupon.setId(couponId);
		coupon = coupon.selectById();
		if (coupon != null) {
			// 领取次数
			coupon.setNums(coupon.getNums() - 1);
			coupon.updateById();

			User user = new User();
			user.setId(userId);
			user = user.selectById();
			if (user == null) {
				return Body.newInstance("用户不存在");
			}
			MyCoupon myCoupon = new MyCoupon();
			myCoupon.setId(Hutool.getId());
			myCoupon.setTime(Hutool.parseDateToString());
			myCoupon.setPhone(user.getPhone());
			myCoupon.setStopTime(coupon.getStopTime());
			myCoupon.setUserId(userId);
			myCoupon.setType(0);
			if (coupon.getDiningId() != null) {
				myCoupon.setDinId(coupon.getDiningId());
			}
			if (coupon.getTotal() != null) {
				myCoupon.setTotal(coupon.getTotal() + "");
			}
			myCoupon.setCouponId(couponId);
			myCoupon.setMoney(coupon.getMoney() + "");
			Boolean flag = myCoupon.insert();
			if (flag) {
				myCoupon.setSum(1);
				List<Coupon> list = new ArrayList<>();
				if (sum != null) {
					String[] appid = sum.split(",");
					for (int i = 0; i < appid.length; i++) {
						String string = appid[i];
						Coupon coupon2 = new Coupon();
						coupon2.setId(string);
						coupon2 = coupon2.selectById();
						if (coupon2 == null) {
							continue;
						}
						MyCoupon myCoupon2 = new MyCoupon();
						EntityWrapper<MyCoupon> ewq = new EntityWrapper<>();
						ewq.eq("coupon_id", string);
						int count = myCoupon2.selectCount(ewq);
						if (count > 0) {
							coupon2.setText(1);
						} else {
							coupon2.setText(0);
						}
						list.add(coupon2);
					}
				}
				return Body.newInstance(list);
			}
			return Body.newInstance("领取失败");
		}
		return Body.newInstance("优惠劵不存在");
	}

	/**
	 * 清除过期优惠劵
	 */
	@PostMapping("deleteMy")
	public Body deleteMy() {
		MyCoupon myCoupon = new MyCoupon();
		List<MyCoupon> list = myCoupon.selectAll();
		if (list.size() > 0) {
			for (MyCoupon myCoupon2 : list) {
				String time = myCoupon2.getStopTime();
				String staime = myCoupon2.getTime();
				String date = DateUtil.dateDiff(time, staime, "yyyy-MM-dd", "h");
				if (Integer.parseInt(date) <= 0) {
					Boolean flag = myCoupon2.deleteById();
					if (flag) {
						return Body.BODY_200;
					}
				}
				continue;
			}
		}
		return Body.BODY_500;
	}

	/**
	 * 清除过期优惠劵0
	 */
	public Body deleteDin() {
		Coupon coupon = new Coupon();
		List<Coupon> list = coupon.selectAll();
		if (list.size() > 0) {
			for (Coupon coupon2 : list) {
				String staime = coupon2.getStopTime();
				String time = coupon2.getCreatetime();
				String date = DateUtil.dateDiff(time, staime, "yyyy-MM-dd", "h");
				if (Integer.parseInt(date) <= 0) {
					Boolean flag = coupon2.deleteById();
					if (flag) {
						return Body.BODY_200;
					}
				}
				continue;
			}
		}
		return Body.BODY_500;
	}

	public static void main(String[] args) {
		System.out.println(com.zcf.common.utils.DateUtils.formatDateNow());
	}

}
